package com.weavernorth.performance;


import org.apache.commons.logging.Log;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.workflow.webservices.*;

public class HandlerAllCreateFlow  extends BaseCronJob {

    private static Log log = org.apache.commons.logging.LogFactory.getLog(HandlerAllCreateFlow.class.getName());
    //创建区域经理考核评分流程
    @Override
    public void  execute (){
        RecordSet rsShop = new RecordSet();
        RecordSet rsShopTwo = new RecordSet();
        RecordSet rsGrade = new RecordSet();
        RecordSet rsClerk = new RecordSet();
        RecordSet rsClerkNum = new RecordSet();
        //查询店铺信息表获取数据
        String strShopsql ="select lsjl,fgs  from uf_dpxx  where (  sfdpphd=1  )  group by lsjl,fgs  ";
        log.info("获取店铺信息的sql:"+strShopsql);
        rsShop.executeQuery(strShopsql);
        while(rsShop.next()) {
            //获取分公司的值
            String strFgs = Util.null2String(rsShop.getString("fgs"));
            log.info("分公司的值：" + strFgs);
            //获取零售经理
            String strLsjl = Util.null2String(rsShop.getString("lsjl"));
            log.info("零售经理的值：" + strLsjl);

            //创建流程的主表
            //存放主表字段的数组
            WorkflowRequestTableField[] wrti = new WorkflowRequestTableField[1];

            //添加分公司字段
            log.info("分公司：" + strFgs);
            wrti[0] = new WorkflowRequestTableField();
            //字段在数据库中的字段名
            wrti[0].setFieldName("fgs");
            //赋值
            wrti[0].setFieldValue(Util.null2String(strFgs));
            wrti[0].setView(true);
            wrti[0].setEdit(true);
            wrti[0].setFieldOrder(0);
            wrti[0].setMand(false);


            WorkflowRequestTableRecord[] wrtri = new WorkflowRequestTableRecord[1];
            wrtri[0] = new WorkflowRequestTableRecord();
            wrtri[0].setWorkflowRequestTableFields(wrti);
            WorkflowMainTableInfo wmi = new WorkflowMainTableInfo();
            wmi.setRequestRecords(wrtri);


            //通过分公司值获取对应零售经理考核评分流程的workflowid的sql
            String strGradeSql = "select qyjlkhlc from uf_khpflcpz where fgs='" + strFgs + "'";
            log.info("获取workflowid的sql：" + strGradeSql);
            rsGrade.executeQuery(strGradeSql);
            //判断是否有对应workflowid
            String strGradeWfId = "";
            if (rsGrade.next()) {
                strGradeWfId = Util.null2String(rsGrade.getString("qyjlkhlc"));
                log.info("零售经理流程的workflowid：" + strGradeWfId);
            }

            //获取明细表长度的sql
//            String strClerkNumSql = "select  count(qyjl) as num  from uf_dpxx where  ( sfdpphd=1  )  and   lsjl='" + strLsjl + "' and fgs='" + strFgs + "'  group by  qyjl";
//            log.info("获取明细表长度的sql：" + strClerkNumSql);
//            rsClerkNum.executeQuery(strClerkNumSql);
//            //明细表长度
//
//            if (rsClerkNum.next()) {
//                intDetailNum = Integer.parseInt(Util.null2String(rsClerkNum.getString("num")));
//                log.info("明细表的长度：" + intDetailNum);
//            }

            //获取店铺信息表店铺id的sql
            String strShopSqlTwo = "select qyjl from uf_dpxx where  ( sfdpphd=1  )  and   lsjl='" + strLsjl + "' and fgs='" + strFgs + "' group by  qyjl ";
            log.info("获取店铺信息其他数据的sql：" + strShopSqlTwo);
            rsShopTwo.executeQuery(strShopSqlTwo);

            //获取店铺id
            String strDpId = "";
            //获取区域经理信息
            String strQyjl = "";

            //创建流程明细表
            //明细表个数为1
            WorkflowDetailTableInfo wdti[] = new WorkflowDetailTableInfo[1];
            //限定明细表的长度
            int intDetailNum = 0;
            intDetailNum=rsShopTwo.getCounts();
            log.info("当前数据长度:"+intDetailNum);
            wrtri = new WorkflowRequestTableRecord[intDetailNum];
            //循环参数
            int intNum = 0;

            while (rsShopTwo.next()) {
               // strDpId = rsShopTwo.getString("dp");
                strQyjl = rsShopTwo.getString("qyjl");

                //有2个字段长度为2
                wrti = new WorkflowRequestTableField[2];

                //添加区域经理姓名字段
                log.info("区域经理姓名：" + strQyjl);
                wrti[0] = new WorkflowRequestTableField();
                //字段在数据库中的字段名
                wrti[0].setFieldName("xm");
                //赋值
                wrti[0].setFieldValue(strQyjl);
                wrti[0].setView(true);
                wrti[0].setEdit(true);

                //获取明细表数据的sql
                String strClerkSql = "select  jobtitle from hrmresource where    id = '" + strQyjl + "'";
                log.info("获取明细数据的sql:" + strClerkSql);
                rsClerk.executeQuery(strClerkSql);
                //获取区域经理职位信息
                String strZw = "";
                if (rsClerk.next()) {
                    strZw = Util.null2String(rsClerk.getString("jobtitle"));
                    //添加区域经理职位字段
                    log.info("区域经理职位：" + strZw);
                    wrti[1] = new WorkflowRequestTableField();
                    //字段在数据库中的字段名
                    wrti[1].setFieldName("zw");
                    //赋值
                    wrti[1].setFieldValue(strZw);
                    wrti[1].setView(true);
                    wrti[1].setEdit(true);
                    wrtri[intNum] = new WorkflowRequestTableRecord();
                    wrtri[intNum].setWorkflowRequestTableFields(wrti);
                }else{
                    //添加区域经理职位字段
                    log.info("区域经理职位为null：");
                    wrti[1] = new WorkflowRequestTableField();
                    //字段在数据库中的字段名
                    wrti[1].setFieldName("zw");
                    //赋值
                    wrti[1].setFieldValue("");
                    wrti[1].setView(true);
                    wrti[1].setEdit(true);

                    wrtri[intNum] = new WorkflowRequestTableRecord();
                    wrtri[intNum].setWorkflowRequestTableFields(wrti);

                }

                intNum++;

            }
            wdti[0] = new WorkflowDetailTableInfo();
            wdti[0].setWorkflowRequestTableRecords(wrtri);
        //基本信息
            WorkflowBaseInfo wbi = new WorkflowBaseInfo();
            //流程的workflowid
            wbi.setWorkflowId(strGradeWfId);

            WorkflowRequestInfo wri = new WorkflowRequestInfo();

            //创建人为零售经理
            wri.setCreatorId(strLsjl);
            //紧急程度
            wri.setRequestLevel("0");
            //请求的名称
            wri.setRequestName("区域经理考核评分流程");
            //停留在提交节点
            wri.setIsnextflow("0");

            //主表数据
            wri.setWorkflowMainTableInfo(wmi);
            wri.setWorkflowBaseInfo(wbi);

            //明细表数据
            //明细表数据为空时，不添加明细表
            if(wdti.length!=0) {
                log.info("sdfasdfasdf"+wdti.length);
                wri.setWorkflowDetailTableInfos(wdti);
            }
            WorkflowServiceImpl wfsi = new WorkflowServiceImpl();
            log.info("创建人的值:"+strLsjl);
            String strRequestid= wfsi.doCreateWorkflowRequest(wri, Integer.parseInt(strLsjl));
            log.info("生成流程的requestid:"+strRequestid);


        }
    }






}
